package com.ketai.dao.impl;

import com.ketai.dao.BaseDao;
import com.ketai.dao.EvaluationDao;
import com.ketai.pojo.Evaluation;
import com.ketai.vo.ActivityVo;
import com.ketai.vo.EvaluationVo;

import javax.naming.NamingException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 高伟华
 * @Description:
 * @Date:Create In 2022-08-11 12:59
 **/
public class EvaluationDaoImpl extends BaseDao implements EvaluationDao {
    @Override
    public List<EvaluationVo> getEvaluation() throws SQLException, NamingException {
        String sql  = "select e.id,title,o.`name`,e.evaluate,date,nickname from evaluation e,c_activity c,organizationbase o,c_user u where e.consul_id = c.id and e.org_id=o.id and e.user_id=u.id";
        ResultSet rs = query(sql, null);
        List<EvaluationVo> evaluations = new ArrayList<>();
        while (rs.next()){
            EvaluationVo evaluation =  new EvaluationVo();
            evaluation.setId(rs.getInt("id"));
            evaluation.setTitle(rs.getString("title"));
            evaluation.setName(rs.getString("name"));
            evaluation.setEvaluate(rs.getString("evaluate"));
            evaluation.setDate(rs.getDate("date"));
            evaluation.setNickname(rs.getString("nickname"));
            evaluations.add(evaluation);
        }
        return evaluations;
    }

    @Override
    public int addEvaluation(Evaluation evaluation) throws SQLException, NamingException {
        String sql = "INSERT INTO `researchtrip`.`evaluation` (`id`, `consul_id`, `org_id`, `evaluate`, `score`, `date`, `user_id`) VALUES (?, '?', ?, '?', '?', '?', '?')";
        Object [] params={evaluation.getId(),evaluation.getCourse_id(),evaluation.getOrg_id(),evaluation.getEvaluate(),evaluation.getScorc(),evaluation.getDate(),evaluation.getUser_id()};
        return curdMethod(sql,params);
    }

    @Override
    public int deleteEvaluationById(String id) throws SQLException, NamingException {
        String sql = "delete from evaluation where id="+id;
        int result = curdMethod(sql,null);
        return 0;
    }


    @Override
    public EvaluationVo getEvaluationById(String id) throws SQLException, NamingException {
        String sql  = "select e.id,title,o.`name`,e.evaluate,date,nickname from evaluation e,c_activity c,organizationbase o,c_user u where e.consul_id = c.id and e.org_id=o.id and e.user_id=u.id";
        Object [] params = {id};
        ResultSet rs=  query(sql,params);
        EvaluationVo evaluation =  new EvaluationVo();
        while (rs.next()){
            evaluation.setId(rs.getInt("id"));
            evaluation.setTitle(rs.getString("title"));
            evaluation.setName(rs.getString("name"));
            evaluation.setEvaluate(rs.getString("evaluate"));
            evaluation.setDate(rs.getDate("date"));
            evaluation.setNickname(rs.getString("nickname"));
        }
        return evaluation;
    }

    @Override
    public int checkEvaluation(String sql, Object[] params) throws SQLException, NamingException {
        int result=curdMethod(sql, params);
        return result;
    }
}
